from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from rest_framework.response import Response
from rest_framework.views import APIView

from api.models import Student, Teacher
from api.serializers import StudentSerializer, StudentDeSerializer, TeacherSerializer, TeacherDeSerializer


@method_decorator(csrf_exempt, name='dispatch')
class UserView(View):

    def get(self, request, *args, **kwargs):
        print("get Success")
        username = request.GET.get("username")
        print(username)

        return HttpResponse("GET OK")

    def post(self, request, *args, **kwargs):
        print("post Success")
        get = request.POST.get("username")
        print(get)
        return HttpResponse("POST OK")


class StudentAPIView(APIView):
    def get(self, request, *args, **kwargs):
        # 通过DRF扩展的方式来获取参数
        print(request.query_params.get("pwd"))  # DRF扩展的获取参数的方式
        stu_id = kwargs.get("id")
        if stu_id:
            # 查询单个
            stu_obj = Student.objects.get(pk=stu_id)
            # 需要进行序列化,然后.data将序列化器中的数据打包成字典返回
            student_serializer = StudentSerializer(stu_obj).data
            return Response({
                "statues": 200,
                "message": "查询单个学生成功",
                "results": student_serializer
            })
        else:
            # 查询所有
            studnet_obj_all = Student.objects.all()
            studnet_all = StudentSerializer(studnet_obj_all, many=True).data
            return Response({
                "status": 200,
                "message": "查询所有学生成功",
                "result": studnet_all
            })

    def post(self, request, *args, **kwargs):
        # 获取前端的数据
        request_data = request.data
        # 前端传递的数据进行入库时,需要进行判断数据的格式是否合法
        if not isinstance(request_data, dict) or request_data == {}:
            return Response({
                "status": 400,
                "message": "参数有误",
            })

        # 需要使用系列化器完成数据库的反序列化
        # 在数据进行反序列化时需要制定关键词 data
        serializer = StudentDeSerializer(data=request_data)
        # 需要对数据进行校验  通过is_vaild()方法传递来的参数进行校验,校验符合的才返回true
        if serializer.is_valid():
            # 调用save()方法进行保存  保存必须重新create()方法
            student_add = serializer.save()
            return Response({
                "status": 200,
                "message": "添加成功",
                "results": StudentSerializer(student_add).data
            })
        else:
            return Response({
                "status": 400,
                "message": "添加失败",
                # 保存失败的信息会包含在 .errors中
                "results": serializer.errors
            })


class TeacherAPIView(APIView):
    def get(self, request, *args, **kwargs):
        # 通过DRF扩展的方式来获取参数
        print(request.query_params.get("pwd"))  # DRF扩展的获取参数的方式
        tea_id = kwargs.get("id")
        if tea_id:
            # 查询单个
            tea_obj = Teacher.objects.get(pk=tea_id)
            # 需要进行序列化,然后.data将序列化器中的数据打包成字典返回
            teacher_serializer = TeacherSerializer(tea_obj).data
            return Response({
                "statues": 200,
                "message": "查询单个老师成功",
                "results": teacher_serializer
            })
        else:
            # 查询所有
            tea_obj_all = Teacher.objects.all()
            tea_all = TeacherSerializer(tea_obj_all, many=True).data
            return Response({
                "status": 200,
                "message": "查询所有老师成功",
                "result": tea_all
            })

    def post(self, request, *args, **kwargs):
        # 获取前端的数据
        print(11111111)
        request_data = request.data
        print(request_data,6565656)
        # 前端传递的数据进行入库时,需要进行判断数据的格式是否合法
        if not isinstance(request_data, dict) or request_data == {}:
            return Response({
                "status": 400,
                "message": "参数有误",
            })

        # 需要使用系列化器完成数据库的反序列化
        # 在数据进行反序列化时需要制定关键词 data
        serializer = TeacherDeSerializer(data=request_data)
        print(serializer,'sdfsdf')
        # 需要对数据进行校验  通过is_vaild()方法传递来的参数进行校验,校验符合的才返回true
        # print(serializer.is_valid(),6554646)
        if serializer.is_valid():
            # 调用save()方法进行保存  保存必须重新create()方法
            teacher_add = serializer.save()
            return Response({
                "status": 200,
                "message": "添加成功",
                "results": TeacherSerializer(teacher_add).data
            })
        else:
            return Response({
                "status": 400,
                "message": "添加失败",
                # 保存失败的信息会包含在 .errors中
                "results": serializer.errors
            })
